// pages/user/user-pay/index.js
const app = getApp()
import {
  request
} from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showQR: false,
    img: '',
    savePhotosAlbum: false,
    list: [],
    page: 1,
    size: 12,
    reachBottom: false,
    tempFilePath:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData()
  },
  // 请求首页数据
  loadData(page) {
    request('/api/user/pay', app.globalData.token, 'get', {
      page: page || 1,
      size: this.data.size
    }).then(res => {
      if (res.code === 200) {
        let reachBottom = false
        if (page && res.data.length < this.data.size) {
          reachBottom = true
        }
        if(reachBottom) {
          wx.showToast({
            title: '已经到底了哦~',
            icon: 'none'
          })
        }
        if (this.data.reachBottom) return
        let list = this.data.list
        if (page) {
          list = [...list, ...res.data]
        } else {
          list = res.data
        }

        this.setData({
          list,
          page: page || 1,
          reachBottom
        })
      } else {
        wx.showToast({
          title: '请求失败，请重进该页面~',
          icon: 'none'
        })
      }
    })
  },

  // 长按保存
  longPress() {
    // console.log(11);
    wx.getSetting({
      withSubscriptions: true,
      success: (res) => {
        console.log(res.authSetting['scope.writePhotosAlbum'])
        if (res.authSetting['scope.writePhotosAlbum'] !== false) {

          wx.saveImageToPhotosAlbum({
            filePath: this.data.tempFilePath,
            success(res) {
              console.log(res, 'saveImageToPhotosAlbum');
              wx.showToast({
                title: '保存图片成功',
                icon: 'success',
                duration: 2000
              })
            }
          })
        } else {
          // console.log(11);
          this.setData({
            savePhotosAlbum: true
          })
        }
      }
    })
  },
  // 打开授权设置
  openSettingTap() {
    const that = this
    if (!this.data.savePhotosAlbum) return
    console.log(222);

    wx.openSetting({
      withSubscriptions: true,
      success(res) {
        console.log(res, 1)
        console.log(res.authSetting['scope.writePhotosAlbum'], 1)
        that.setData({
          savePhotosAlbum: !res.authSetting['scope.writePhotosAlbum']
        })
      },
      fail: err => {
        console.log(err)
      }
    })

  },
  handelImgStatus(e) {
    console.log(e.currentTarget.dataset.index, 'id')
    // e.currentTarget.dataset.id && request('/api/user/pay?id=' + e.currentTarget.dataset.id, app.globalData.token, 'put').then(res => {
      if(e.currentTarget.dataset.index !== void 0) {
        this.setData({
          img: this.data.list[e.currentTarget.dataset.index].img
        })
        wx.downloadFile({
          url: this.data.list[e.currentTarget.dataset.index].img,
          success: (res) => {
            console.log(res, 'res');
            this.setData({
              tempFilePath: res.tempFilePath
            })
          }
        })
      }
    // })
    
    this.setData({
      showQR: !this.data.showQR
    })
  },
  onReachBottom() {
    this.loadData(this.data.page + 1)
  }
})